var XMLHttpRequestObject = createXMLHttpRequestObject();
var filesadded=""; //list of files already added
var loadedobjects=""
var timerId=null;
var timeout=10;
    
    
function createXMLHttpRequestObject()
{
  var XMLHttpRequestObject = false;
  
  try
  {
    XMLHttpRequestObject = new XMLHttpRequest();
  }
  catch(e)
  {
    var aryXmlHttp = new Array(
                               "MSXML2.XMLHTTP",
                               "Microsoft.XMLHTTP",
                               "MSXML2.XMLHTTP.6.0",
                               "MSXML2.XMLHTTP.5.0",
                               "MSXML2.XMLHTTP.4.0",
                               "MSXML2.XMLHTTP.3.0"
                               );
    for (var i=0; i<aryXmlHttp.length && !XMLHttpRequestObject; i++)
    {
      try
      {
        XMLHttpRequestObject = new ActiveXObject(aryXmlHttp[i]);
      } 
      catch (e) {}
    }
  }
  
  if (!XMLHttpRequestObject)
  {
    alert("Error: failed to create the XMLHttpRequest object.");
  }
  else 
  {
    return XMLHttpRequestObject;
  }
  return false;
}

function loadjscssfile(filename, filetype){
 if (filetype=="js")
 { //if filename is a external JavaScript file
      var num=Math.random();
      var fileref=document.createElement('script');
      fileref.setAttribute("type","text/javascript");
      fileref.setAttribute("src", filename+'?par='+num);
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link");
  fileref.setAttribute("rel", "stylesheet");
  fileref.setAttribute("type", "text/css");
  fileref.setAttribute("href", filename);
 }
 if (typeof fileref!="undefined")
 {
    document.getElementsByTagName("head")[0].appendChild(fileref);
    
 }
}

function checkloadjscssfile(filename, filetype){
 if (filesadded.indexOf("["+filename+"]")==-1)
 {
  loadjscssfile(filename, filetype);
  filesadded+="["+filename+"]"; //List of files added in the form "[filename1],[filename2],etc"
 }
 else
     ;
    // alert("file already added!");
}


function getData_Over(dataSource, divID, ifLoading)
{
  var ret=false;
  var objDiv;
  if(XMLHttpRequestObject)
  {
    dataSource += "&parm="+new Date().getTime();
    
    XMLHttpRequestObject.onreadystatechange = function()
    {
      try
      {
        if (XMLHttpRequestObject.readyState == 4 &&
            XMLHttpRequestObject.status == 200)
        {
          objDiv = document.getElementById(divID);
          objDiv.innerHTML = XMLHttpRequestObject.responseText;
          
          ret=true;
        }
        else
        {
          if(ifLoading)
          {
            objDiv = document.getElementById(divID);
            objDiv.innerHTML = "<html>\n\
                                <body align=\"center\">\n\
                                    <p align=\"center\"><img src=\"images/loading.gif\" width=\"220\" height=\"19\" /></p>\n\
                                </body>\n\
                                </html>";
          }
        }
      }
      catch(e){
	document.write("getData: XMLHttpRequestObject.readyState Error");
	ret=false;
      }
    }
    
    XMLHttpRequestObject.open("GET", dataSource,false);
    XMLHttpRequestObject.send(null);
    //loadFileJsRubrica();
    ret=true;
    
  }
  return ret;
}


function reload() { 
alert("cdcdcdora si");
        var scripts = document.getElementsByTagName("script"); 
        var head = document.getElementsByTagName("head")[0]; 

        var newScripts = []; 
        var removeScripts = []; 
        for(var i=0; i < scripts.length; i++) { 
        var parent = scripts[i].parentNode; 
        if( parent == head  && scripts[i].src) { 
            var newScript = {}; 
            newScript.src = scripts[i].src; 
            newScript.innerHTML = scripts[i].innerHTML; 
            newScripts.push(newScript); 
            removeScripts.push(scripts[i]); 
        } 
        } 
        for(i=0; i < removeScripts.length; i++) { 
        head.removeChild(removeScripts[i]); 
        } 
        for(i=0; i < newScripts.length; i++) 
        { 
            var script = document.createElement("script"); 
            script.src = newScripts[i].src; 
            script.type = "text/javascript"; 
            script.innerHTML = newScripts[i].innerHTML; 
            head.appendChild(script); 
        }
} 


function loadobjs(){
if (!document.getElementById)
    return
for (i=0; i<arguments.length; i++){
    var file=arguments[i];
    var fileref="";
    //if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
        if (file.indexOf(".js")!=-1){ //If object is a js file
            fileref=document.createElement('script')
            fileref.setAttribute("type","text/javascript");
            fileref.setAttribute("src", file);
        }
        else if (file.indexOf(".css")!=-1){ //If object is a css file
            fileref=document.createElement("link")
            fileref.setAttribute("rel", "stylesheet");
            fileref.setAttribute("type", "text/css");
            fileref.setAttribute("href", file);
        }
   // }
    if (fileref!=""){
        document.getElementsByTagName("head").item(0).appendChild(fileref)
        loadedobjects+=file+" " //Remember this object as being already added to page
    }
}
}

function getData(dataSource, divID, ifLoading)
{   
  if(XMLHttpRequestObject)
  {
    dataSource += "&parm="+new Date().getTime();
    
    XMLHttpRequestObject.open("GET", dataSource);
    XMLHttpRequestObject.onreadystatechange = function()
    {
      try
      {
          //alert("cdcdcdcdcdcd");
        if (XMLHttpRequestObject.readyState == 4 &&
            XMLHttpRequestObject.status == 200)
        {
          var objDiv = document.getElementById(divID);
          objDiv.innerHTML = XMLHttpRequestObject.responseText;
        }
        else
        {
          if(ifLoading)
          {
            var objDiv = document.getElementById(divID);
            objDiv.innerHTML = "<img src=loading.gif>";
          }
        }
      }
      catch(e){document.write("getData: XMLHttpRequestObject.readyState Error");}
    }
    try
    {
      XMLHttpRequestObject.send(null);
    }
    catch(e){document.write("getData: XMLHttpRequestObject.onreadystatechange Error");}
  }
}

function postData(dataSource, divID, ifLoading)
{
  if(XMLHttpRequestObject)
  {
    XMLHttpRequestObject.open("POST", dataSource);
    XMLHttpRequestObject.setRequestHeader("Method", "POST " + dataSource + " HTTP/1.1");
	  XMLHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    XMLHttpRequestObject.onreadystatechange = function()
    {
      try
      {
        if (XMLHttpRequestObject.readyState == 4 &&
            XMLHttpRequestObject.status == 200)
        {
          var objDiv = document.getElementById(divID);
          objDiv.innerHTML = XMLHttpRequestObject.responseText;
        }
        else
        {
          if(ifLoading)
          {
            var objDiv = document.getElementById(divID);
            objDiv.innerHTML = "<img src=loading.gif>";
          }
        }
      }
      catch(e){document.write("postData: XMLHttpRequestObject.readyState Error");}
    }
    
    dataSource += "&parm="+new Date().getTime();
    try
    {
      XMLHttpRequestObject.send(dataSource);
    }
    catch(e){document.write("postData: XMLHttpRequestObject.onreadystatechange Error");}
  }
}



function loadArchivioTab(dataSource, divID)
{
    var ret=getData_Over(dataSource, divID, true);
    if(ret)
            loadFileJsRubrica();
//    loadobjs("files/js/jquery-1.4.2.js");
//    loadobjs("files/js/jquery.tools.min.js");
//    loadobjs("files/js/jquery.tablesorter.js");
//    loadobjs("files/js/jquery.tablesorter.pager.js");
//    loadobjs("files/js/overlay.js");
//    loadobjs("style/themes/blue/style.css");
    return ret;
}

function loadPasswordTab(dataSource, divID)
{
    var ret=getData_Over(dataSource, divID, true);
    var d = new Date();
    var num=d.getMilliseconds();
    if(ret)
        {
            loadobjs("files/js/jquery.js?id="+num);
            loadobjs("files/js/ajax_chpasswd.js?id="+num);
            loadobjs("style/themes/blue/style.css?id="+num);
        }
            
//    loadobjs("files/js/jquery-1.4.2.js");
//    loadobjs("files/js/jquery.tools.min.js");
//    loadobjs("files/js/jquery.tablesorter.js");
//    loadobjs("files/js/jquery.tablesorter.pager.js");
//    loadobjs("files/js/overlay.js");
//    loadobjs("style/themes/blue/style.css");
    return ret;
}

function loadArchivioTab_Old(dataSource, divID)
{
    var ret=getData_Over(dataSource, divID, true);
    if(ret)
        {
            loadobjs("files/js/jquery-1.4.2.js");
            //loadobjs("files/js/jquery.tools.min.js");
            loadobjs("files/js/jquery.tablesorter.js");
            loadobjs("files/js/jquery.tablesorter.pager.js");
            loadobjs("files/js/overlay.js");
            loadobjs("style/themes/blue/style.css");
        }
    return ret;
}
/*function include_js(file) {
    var html_doc = document.getElementById('showTab');
    js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', file);
    html_doc.appendChild(js);

    js.onreadystatechange = function () {
        if (js.readyState == 'complete') {
//            alert('JS onreadystate fired');
        }
    }

    js.onload = function () {
//        alert('JS onload fired');
    }
    return false;
}*/
function loadFileJsArchivio()
{
    loadMyScript("files/js/jquery.tablesorter.js");
    loadMyScript("files/js/jquery.tablesorter.pager.js");
    loadMyScript("files/js/overlay.js");

}
function loadFileJsRubrica()
{

    loadMyScript("files/js/jquery-1.4.2.js");
    loadMyScript("files/js/jquery.tools.min.js");
    loadMyScript("files/js/jquery.tablesorter.js");
    loadMyScript("files/js/jquery.tablesorter.pager.js");
    loadMyScript("files/js/overlay.js");

//    loadMyScript("style/themes/blue/style.css");
}

function invokeScript(divid)
{
var scriptObj = divid.getElementsByTagName("SCRIPT");
var len = scriptObj.length;
for(var i=0; i<len; i++)
{
var scriptText = scriptObj[i].text;
var scriptFile = scriptObj[i].src
var scriptTag = document.createElement("SCRIPT");
if ((scriptFile != null) && (scriptFile != "")){
scriptTag.src = scriptFile;
}
scriptTag.text = scriptText;
if (!document.getElementsByTagName("HEAD")[0]) {
document.createElement("HEAD").appendChild(scriptTag)
}
else {
document.getElementsByTagName("HEAD")[0].appendChild(scriptTag);
}
}
}

function loadMyScript(file) {
    if ( !(document.createElement) ) {
    alert( 'Browser does not support dynamic script loading' );
    return;
    }
    //document.getElementById('progress').innerHTML = 'Loading';
    var jsMyNode = document.createElement('script');
    jsMyNode.type = 'text/javascript';
    jsMyNode.src = file;
    jsMyNode.id = 'jsMyScript';
    var jsNode = document.getElementsByTagName('script')[0];//getElementById('jsPlaceHolder');
    var headNode = document.getElementsByTagName('head')[0];
    var newJsNode = null;
    if ( !!(jsNode) && !!(headNode) )
  //    newJsNode = document.getElementsByTagName("head")[0].replaceChild( jsMyNode, jsNode );
          newJsNode = headNode.appendChild(jsMyNode);//replaceChild( jsMyNode, jsNode );
    if ( !(newJsNode) )
    //alert( 'Program load failure' );
    return;
}


function start(){
    if ( !( this.myObj ) ) {
    alert ( 'Called too early, there is no this.myObj!' );
    return;
    }
    document.getElementById('progress').innerHTML = 'Started!';
    alert ( 'I am happily running!' );
    return;
}

function loadWait() {
    // Dynamically loaded code is not in our namespace until the entire page is loaded,
    // <body onload="xxx();"> -> xxx() would not see the loaded coded necessarily, so we wait..
    if ( !!( timerId ) ) {
        if ( timeout == 0 ) {
        document.getElementById('progress').innerHTML = "Load failed.";
        alert( "Program load timeout" );
        return;
        }
        if ( !!( this.myObj ) ) {
        start(); // Ok to launch the actual program
        return;
        }
        timeout -= 1;
        }
    document.getElementById('progress').innerHTML += '.';
    timerId = setTimeout( "loadWait()", 1000); // You can set this to 100 if you do not print d
    return;
}
